package com.shishi.fast.common.advice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shishi.fast.common.annotation.LoginLog;
import com.shishi.fast.common.constant.LogType;
import com.shishi.fast.common.util.IpUtil;
import com.shishi.fast.common.vo.SysLogVO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 类描述：登录日志，切面处理类
 * Created by liuhaifeng on 2022/3/17.
 */
@Slf4j
@Aspect
@Component
public class SysLoginLogAspect {

    @Pointcut("@annotation(com.shishi.fast.common.annotation.LoginLog)")
    public void logLoginPointCut() {
    }

    private SysLogVO universalVOLoading(ProceedingJoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLogVO sysLogVO = new SysLogVO();
        LoginLog loginLog = method.getAnnotation(LoginLog.class);
        if (loginLog != null) {
            //注解上的描述
            sysLogVO.setOperation(loginLog.value());
        }
        //获取当前请求对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            //获取request
            HttpServletRequest request = attributes.getRequest();
            //用户IP地址
            sysLogVO.setIp(IpUtil.getIpAddr(request));
        }
        return sysLogVO;
    }

    // 登录成功
    private void saveLoginSuccessLog(ProceedingJoinPoint joinPoint) {
        SysLogVO sysLogVO = universalVOLoading(joinPoint);
        sysLogVO.setType(LogType.LOGIN.getType());
        sysLogVO.setStatus(1);
    }

}
